{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## list操作及for循环"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （1） 交叉合并两个list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "学号 姓名 成绩\n",
      "202021100000 小明 90\n",
      "202021100001 小亮 98\n",
      "202021100002 小红 80\n"
     ]
    }
   ],
   "source": [
    "sno = ['202021100000', '202021100001', '202021100002']\n",
    "name = ['小明', '小亮', '小红']\n",
    "score = [90, 98, 80]\n",
    "\n",
    "print('学号', '姓名', '成绩')\n",
    "for i in range(len(sno)): # length\n",
    "    print(sno[i], name[i], score[i])\n",
    "# print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （2） 1-1/3+1/5-1/7...前n项和，打印4倍该和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.139592655589785\n",
      "3.1415726535897814\n"
     ]
    }
   ],
   "source": [
    "t = []\n",
    "for i in range(1000):\n",
    "    if i % 2 == 0:\n",
    "        t.append(1 / (i + 1))\n",
    "# t = 1 3 5 7 9\n",
    "#     0 1 2 3 4\n",
    "s = 0\n",
    "for i in range(len(t)):\n",
    "    if i % 2 == 0:\n",
    "        s += t[i]\n",
    "    else:\n",
    "        s += -t[i]\n",
    "print(s * 4)\n",
    "\n",
    "t = []\n",
    "for i in range(100000):\n",
    "    if i % 2 == 0:\n",
    "        t.append(1 / (i + 1))\n",
    "# t = 1 3 5 7 9\n",
    "#     0 1 2 3 4\n",
    "s = 0\n",
    "for i in range(len(t)):\n",
    "    if i % 2 == 0:\n",
    "        s += t[i]\n",
    "    else:\n",
    "        s += -t[i]\n",
    "print(s * 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （3） n个[m, k]之间的随机数，输出平方根"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "n: 4\n",
      "m: 1\n",
      "k: 100\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.4641016151377544\n",
      "9.848857801796104\n",
      "8.831760866327848\n",
      "6.855654600401044\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import math\n",
    "n = int(input('n:'))\n",
    "m = int(input('m:'))\n",
    "k = int(input('k:'))\n",
    "\n",
    "for _ in range(n):\n",
    "    print(math.sqrt(random.randint(m, k)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### （4） 求list数的最大、最小、平均值，不适用内置函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请指定长度:  4\n",
      "第1个数:  1\n",
      "第2个数:  -2\n",
      "第3个数:  3\n",
      "第4个数:  44\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最大: 44\n",
      "最小: -2\n",
      "平均: 11.5\n"
     ]
    }
   ],
   "source": [
    "l = []\n",
    "\n",
    "length = int(input('请指定长度: '))\n",
    "for i in range(length):\n",
    "    t = int(input('第' + str(i+1) + '个数: '))\n",
    "    l.append(t)\n",
    "    \n",
    "\n",
    "def my_max(arr):\n",
    "    m = arr[0]\n",
    "    for item in arr: # i index item\n",
    "        if item > m:\n",
    "            m = item\n",
    "    return m\n",
    "print('最大:', my_max(l))\n",
    "\n",
    "def my_min(arr):\n",
    "    m = arr[0]\n",
    "    for item in arr: # i index item\n",
    "        if item < m:\n",
    "            m = item\n",
    "    return m\n",
    "print('最小:', my_min(l))\n",
    "\n",
    "def my_avg(arr):\n",
    "    s = 0\n",
    "    for item in arr: # i index item\n",
    "        s += item\n",
    "    return s / len(arr)\n",
    "print('平均:', my_avg(l))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
